home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 1997 May / PC Plus Super CD Issue 127 (May 1997).iso / delphi1 / lesson4 / todolist / strutils.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1996-01-12  |  3.3 KB  |  114 lines

  1. unit Strutils;
  2. { handy string handling procedures }
  3.  
  4. interface
  5.  
  6. const
  7.    separators = [' ', '.', ',', '!', '?',';',':' ];
  8.  
  9. type
  10.    charSet = set of char;
  11.  
  12. procedure fronttoken( var s, first, rest : string );
  13.    { parse string 's' into the 1st word 'first' and the remainder 'rest' }
  14. function trimLeft( s : string ) : string;
  15.    { remove leading separators from string 's' }
  16. function trimRight( s : string ) : string;
  17.    { remove trailing separators from string 's' }
  18. function trimEnds( s : string ) : string;
  19.    { trim separators from both left and right of string }
  20.  
  21.  
  22. implementation
  23. function removeLeadChars( s : string; chars : charSet ) : string;
  24.    { trims string 's' by removing chars in charSet 'chars'
  25.    e.g. charSet might be separators. So if s = ' .hello'
  26.    this function would return: 'hello'. }
  27. var
  28.    i : integer;
  29.    cleanS : string;
  30. begin
  31.    cleanS := '';
  32.    i := 1;
  33.        { count to end of characters found in charSet }
  34.    while (s[i] in chars) and (i < length(s)) do
  35.        inc(i);
  36.        { then return a copy of the remainder of the string }
  37.        { unless the string is empty in which case return it as such }
  38.    if i <= length(s) then cleanS := copy(s, i, length(s));
  39.    removeLeadChars := cleanS;
  40. end;
  41.  
  42. function removeTrailingChars( s : string; chars : charSet ) : string;
  43. var
  44.    endOfS : integer;
  45.    cleanS : string;
  46. begin
  47.    cleanS := '';
  48.    endOfS := length(s);
  49.        { count backward from end of characters found in charSet }
  50.    while (s[endOfS] in chars) and (endOfS <> 0 ) do
  51.        dec(endOfS);
  52.        { then return a copy of the string minus the unwanted trailing chars }
  53.        { unless the string is empty in which case return it as such }
  54.    if endOfS <> 0 then cleanS := copy(s, 1, endOfS);
  55.    removeTrailingChars := cleanS;
  56. end;
  57.  
  58.  
  59. function trimLeft( s : string ) : string;
  60. { remove leading separators from string 's' }
  61. begin
  62.    trimLeft := removeLeadChars( s, separators );
  63. end;
  64.  
  65. function trimRight( s : string ) : string;
  66. { remove trailing separators from string 's' }
  67. begin
  68.    trimRight := removeTrailingChars( s, separators );
  69. end;
  70.  
  71. function trimEnds( s : string ) : string;
  72. { trim separators from both left and right of string }
  73. var
  74.    s1 : string;
  75. begin
  76.    s1 := s;
  77.    s1 := trimLeft( s1 );
  78.    s1 := trimRight( s1 );
  79.    trimEnds := s1;
  80. end;
  81.  
  82. procedure fronttoken( var s, first, rest : string );
  83. { Given a string, 's', parse out the first word as 'first' and
  84.   the remainder of the string as 'rest' }
  85. var
  86.    i : integer;
  87.    endOfWord : boolean;
  88.    trimmed_s : string;
  89. begin
  90.    endOfWord := false;
  91.    i := 1;
  92.    first := '';
  93.    rest := '';
  94.    trimmed_s := trimEnds( s );
  95.        { if s is a blank string then don't try to process it }
  96.    if length(trimmed_s) = 0 then endOfWord := true;
  97.        { when a a separator is found, endOfWord is true. So parse
  98.          out first word and rest of string. }
  99.    while not endOfWord do
  100.    begin
  101.        if trimmed_s[i] in separators then
  102.        begin
  103.            endOfWord := true;
  104.            first := copy( trimmed_s, 1, i-1 );
  105.                { if there is more than one separator after 'first', remove
  106.                them from the front of 'rest' }
  107.            rest := trimLeft( copy(trimmed_s, i+1, length(trimmed_s)) );
  108.        end
  109.        else inc(i);
  110.    end;
  111. end;
  112.  
  113. end.
  114.